/*
 * Statistics on a matrix of numbers.
 * The matrix is required to have line breaks after each row.
 * Note that zero entries are not counted (assumed to map to movies we didn't
 * find in Wikipedia).
 *
 * First argument is bucket size (positive).
 * Second argument is "diag" to include the diagonal, or "nodiag" to exclude
 * the diagonal. If it is not specified, the diagonal is included.
 * Takes the matrix on standard in.
 * Produces statistics on standard out.
 */
#include <iostream>
#include <sstream>
#include <string>
#include "u1.h"
using namespace std;

int main(int argc, char **argv)
{
  if (argc < 2) {
    cerr<<"See comments for usage."<<endl;
    return EXIT_FAILURE;
  }
  double delta = atof(argv[1]);
  if (delta <= 0) {
    cerr<<"Bucket size must be positive."<<endl;
    return EXIT_FAILURE;
  }
  bool includeDiagonal = true;
  if (argc > 2) {
    string diag_string = argv[2];
    if (diag_string == "diag")
      includeDiagonal = true;
    else if (diag_string == "nodiag")
      includeDiagonal = false;
  }

  string line;
  int row = 0, cols = -1;
  Histogram<double> hist(delta);
  StatCounter stat;
  while (getline(cin,line)) {
    ++row;

    // Process row.
    istringstream is(line);
    Histogram<double> rowHist(delta);
    StatCounter rowStat;
    double entry;
    int col = 0;
    while (is >> entry) {
      ++col;
      if (row == col && !includeDiagonal)
        continue;
      if (entry == 0.0) // No match.
        continue;
      hist.record(entry);
      stat(entry);
      rowHist.record(entry);
      rowStat(entry);
    }

    // Sanity check: make sure all rows have same # of entries.
    if (cols == -1) {
      cols = col;
    } else if (col != cols) {
      cerr<<"Row "<<row<<" contains "<<col<<" columns; expected "
        <<cols<<"."<<endl;
    }

    // Output row stats.
    cout<<row
      <<":min="<<rowStat.min()
      <<",max="<<rowStat.max()
      <<",mean="<<rowStat.mean()
      <<",stddev="<<rowHist.getStdDev()
      <<",bins="<<rowHist<<endl;
  }
  cout
    <<"min="<<stat.min()
    <<",max="<<stat.max()
    <<",mean="<<stat.mean()
    <<",stddev="<<hist.getStdDev()
    <<",bins="<<hist<<endl;

  return EXIT_SUCCESS;
}

/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

